<?php
class PageSaveEditAction extends AdminAction
{
	public function __construct()
	{
		parent::__construct();
		$this->responseType = 'json';
	}
	
	protected function afterConstruct()
	{
		$this->parseDtds('page','edit');
	}
	
	public function execute()
	{
		$pageService	= new PageService();
		$id				= $this->context->post('id');
		$data			= $this->getValidData();
		//检查url是否唯一
		$tmp			= $pageService->getByUrl($data['url']);
		if($tmp && $tmp['id']!=$id)
		{
			$this->data['error_messages']['url']	= array('error'=>'该URL已经被其他页面使用');
		}
		if(empty($this->data['error_messages']))
		{
			if(is_numeric($id) && $id)
			{
				$pageService->update($id,$data);
				$this->code	= 200;
				$this->data['forward'] = C('LtUrl')->generate('Page', 'Index');
			}
			else
			{
				$id	= $pageService->add($data);
				if($id)
				{
					$this->code	= 200;
					$this->data['forward'] = C('LtUrl')->generate('Page', 'Index');
				}
				else
				{
					$this->code	= 400;
				}
			}
		}
		else
		{
			$this->code	= 400;
		}
	}
}
